package com.starview.star.operators.arith.runtime;

import java.math.BigDecimal;

import com.starview.platform.data.EvaluationException;
import com.starview.platform.data.IFunction;
import com.starview.platform.data.IValue;
import com.starview.platform.data.type.IType;
import com.starview.platform.data.type.StandardTypes;
import com.starview.platform.data.type.TypeUtils;
import com.starview.platform.data.value.Factory;
import com.starview.star.operators.CafeEnter;



public class BigNumUnary
{
  public static class DecimalAbs implements IFunction
  {
    @CafeEnter
    public static BigDecimal enter(BigDecimal s1)
    {
      return s1.abs();
    }

    @Override
    public IValue enter(IValue... args) throws EvaluationException
    {
      return Factory.newDecimal(enter(Factory.decimalValue(args[0])));
    }

    @Override
    public IType getType()
    {
      IType decimalType = StandardTypes.rawDecimalType;
      return TypeUtils.functionType(decimalType, decimalType);
    }
  }

  public static class DecimalUMinus implements IFunction
  {
    @CafeEnter
    public static BigDecimal enter(BigDecimal s1)
    {
      return s1.negate();
    }

    @Override
    public IValue enter(IValue... args) throws EvaluationException
    {
      return Factory.newDecimal(enter(Factory.decimalValue(args[0])));
    }

    @Override
    public IType getType()
    {
      IType decimalType = StandardTypes.rawDecimalType;
      return TypeUtils.functionType(decimalType, decimalType);
    }
  }

  public static class DecimalRandom implements IFunction
  {
    @CafeEnter
    public static BigDecimal enter(BigDecimal s1)
    {
      double random = Math.random();
      return s1.multiply(new BigDecimal(random));
    }

    @Override
    public IValue enter(IValue... args) throws EvaluationException
    {
      return Factory.newDecimal(enter(Factory.decimalValue(args[0])));
    }

    @Override
    public IType getType()
    {
      IType decimalType = StandardTypes.rawDecimalType;
      return TypeUtils.functionType(decimalType, decimalType);
    }
  }

  public static class DecimalSqrt implements IFunction
  {
    @CafeEnter
    public static BigDecimal enter(BigDecimal s1) throws EvaluationException
    {
      throw new EvaluationException("sqrt not implemented for decimal numbers");
    }

    @Override
    public IValue enter(IValue... args) throws EvaluationException
    {
      return Factory.newDecimal(enter(Factory.decimalValue(args[0])));
    }

    @Override
    public IType getType()
    {
      IType decimalType = StandardTypes.rawDecimalType;
      return TypeUtils.functionType(decimalType, decimalType);
    }
  }
}
